package vip.mall.modular.orders.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.mall.common.result.CommonPageResult;
import vip.mall.common.utils.OrderNumberGen;
import vip.mall.modular.goods.entity.Goods;
import vip.mall.modular.goods.service.GoodsService;
import vip.mall.modular.orders.entity.Orders;
import vip.mall.modular.orders.mapper.OrdersMapper;
import vip.mall.modular.orders.params.AddOrderParams;
import vip.mall.modular.orders.params.PageOrderParams;
import vip.mall.modular.orders.service.OrdersService;
import vip.mall.modular.orders.vo.OrderTableVo;
import vip.mall.modular.user.entity.User;
import vip.mall.modular.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Asus
 * @Date 2025/2/10 15:27
 * @Descripition
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Resource
    private UserService userService;
    @Resource
    private GoodsService goodsService;


    @Override
    public CommonPageResult pageList(PageOrderParams pageOrderParams) {
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(pageOrderParams.getOrderNumber()))
            lambdaQueryWrapper.eq(Orders::getOrderNumber, pageOrderParams.getOrderNumber());
        if (ObjectUtil.isNotEmpty(pageOrderParams.getUserId()))
            lambdaQueryWrapper.eq(Orders::getUserId, pageOrderParams.getUserId());
        Page<Orders> page = new Page<>(pageOrderParams.getPageNum(), pageOrderParams.getPageSize());
        Page<Orders> ordersPage = this.page(page,lambdaQueryWrapper);
        List<OrderTableVo> orderTableVos = new ArrayList<>();
        for (Orders order : ordersPage.getRecords()) {
            OrderTableVo orderTableVo = BeanUtil.copyProperties(order, OrderTableVo.class);
            User user = userService.getById(orderTableVo.getUserId());
            if (ObjectUtil.isNotNull(user))
                orderTableVo.setUserRealName(user.getRealName());
            Goods goodsServiceById = goodsService.getById(orderTableVo.getGoodsId());
            if (ObjectUtil.isNotNull(goodsServiceById)) {
                orderTableVo.setGoodsName(goodsService.getById(orderTableVo.getGoodsId()).getGoodsName());
                orderTableVo.setGoodsImg(goodsService.getById(orderTableVo.getGoodsId()).getGoodsImg());
            }
            orderTableVos.add(orderTableVo);
        }
        if (ObjectUtil.isNotNull(ordersPage)) {
            return CommonPageResult.builder()
                    .data(orderTableVos)
                    .pageNum((int) ordersPage.getCurrent() + 1)
                    .pageSize((int) ordersPage.getSize())
                    .total(ordersPage.getTotal())
                    .totalPage((int) ordersPage.getPages())
                    .build();
        }
        return null;
    }

    @Override
    public boolean add(AddOrderParams addOrderParams) {
        if (ObjectUtil.isNull(addOrderParams)) {
            return false;
        }
        Orders orders = BeanUtil.copyProperties(addOrderParams, Orders.class);
        orders.setOrderNumber(OrderNumberGen.getOrderIdByTime());
        return this.save(orders);
    }

    @Override
    public List<OrderTableVo> cartGet(String userId) {
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(Orders::getUserId, userId);
            lambdaQueryWrapper.eq(Orders::getOrderStatus, 0);
        }
        List<Orders> ordersList = list(lambdaQueryWrapper);
        List<OrderTableVo> orderTableVos = new ArrayList<>();
        for (Orders order : ordersList) {
            OrderTableVo orderTableVo = BeanUtil.copyProperties(order, OrderTableVo.class);
            User user = userService.getById(orderTableVo.getUserId());
            if (ObjectUtil.isNotNull(user))
                orderTableVo.setUserRealName(user.getRealName());
            Goods goodsServiceById = goodsService.getById(orderTableVo.getGoodsId());
            if (ObjectUtil.isNotNull(goodsServiceById)) {
                orderTableVo.setGoodsName(goodsService.getById(orderTableVo.getGoodsId()).getGoodsName());
                orderTableVo.setGoodsImg(goodsService.getById(orderTableVo.getGoodsId()).getGoodsImg());
            }
            orderTableVos.add(orderTableVo);
        }
        if (ObjectUtil.isNotNull(orderTableVos)) {
            return orderTableVos;
        }
        return null;
    }

    @Override
    public boolean editCart(List<String> ids) {
        for (String id : ids) {
            Orders orders = getById(id);
            orders.setOrderStatus(1);
            updateById(orders);
        }
        return true;
    }

    @Override
    public void edit(OrderTableVo orders) {
        Orders properties = BeanUtil.copyProperties(orders, Orders.class);
        updateById(properties);
    }
}
